package ch.ffhs.ks.ui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class SimpleFormPanel extends JPanel 
{ 
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private JPanel form; 
	private final int LABEL_WIDTH; 
	private final int LABEL_ALIGNMENT;

	public SimpleFormPanel(int labelWidth, int labelAlignment) 
	{ 
		super(new BorderLayout()); 
		LABEL_WIDTH = labelWidth; 
		LABEL_ALIGNMENT = labelAlignment;

		form = new JPanel(new GridLayout(0,1,0,5)); 
		add(form, BorderLayout.NORTH);
	}

	@Override
	public void setEnabled(boolean enabled) {

		for(int i = 0; i < form.getComponentCount(); i ++)
		{
			Component c = form.getComponent(i);

			if(c instanceof JPanel)
			{
				JPanel row = (JPanel)c;

				for(int j = 0; j < row.getComponentCount(); j ++)
				{
					row.getComponent(j).setEnabled(enabled);
				}

			}

		}

	}

	public JTextField addRow(String labelText) 
	{ 
		JPanel row = new JPanel(new BorderLayout(5,0)); 
		form.add(row);

		JLabel label = new JLabel(labelText, LABEL_ALIGNMENT); 
		Dimension size = label.getPreferredSize(); 

		label.setPreferredSize(new Dimension(LABEL_WIDTH, size.height)); 
		row.add(label, BorderLayout.WEST);

		JTextField textField = new JTextField(); 
		row.add(textField, BorderLayout.CENTER); 

		return textField;

	}

	public JPasswordField addPasswordRow(String labelText)
	{
		JPanel row = new JPanel(new BorderLayout(5,0)); 
		form.add(row);

		JLabel label = new JLabel(labelText, LABEL_ALIGNMENT); 
		Dimension size = label.getPreferredSize(); 

		label.setPreferredSize(new Dimension(LABEL_WIDTH, size.height)); 
		row.add(label, BorderLayout.WEST);

		JPasswordField textField = new JPasswordField(); 
		row.add(textField, BorderLayout.CENTER); 

		return textField;

	}

	public JComboBox addComboBox(String labelText) {

		JPanel row = new JPanel(new BorderLayout(5,0)); 
		form.add(row);

		JLabel label = new JLabel(labelText, LABEL_ALIGNMENT); 
		Dimension size = label.getPreferredSize(); 

		label.setPreferredSize(new Dimension(LABEL_WIDTH, size.height)); 
		row.add(label, BorderLayout.WEST);

		JComboBox textField = new JComboBox(); 
		row.add(textField, BorderLayout.CENTER); 

		return textField;


	}
}